package br.com.dwii.tpg2.servlet;

import br.com.dwii.tpg2.entidade.Caracteristica;
import br.com.dwii.tpg2.entidade.Usuario;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Diego
 */
public class UsuarioSrv extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String url = "index.jsp";
        
        // Usuário
        Usuario usr = new Usuario();
        usr.setNome(String.valueOf(request.getParameter("nome")));
        usr.setCpf(String.valueOf(request.getParameter("cpf")).replace(".", "").replace("-", ""));
        if (request.getParameter("fone") != null) {
                usr.setFone(String.valueOf(request.getParameter("fone")).replace("(", "").replace(")", "").replace("-", ""));
            }
        usr.setEmail(String.valueOf(request.getParameter("email")));
        usr.setSenha(String.valueOf(request.getParameter("senha")));
        
        // Caracteristica
        Caracteristica carac = new Caracteristica();
        carac.setFlagTipoAnimal(String.valueOf(request.getParameter("tipo_animal")));
        carac.setFlagSexo(String.valueOf(request.getParameter("sexo")));
        carac.setFlagIdade(String.valueOf(request.getParameter("idade")));
        carac.setFlagPorte(String.valueOf(request.getParameter("porte")));
        carac.setFlagPelagem(String.valueOf(request.getParameter("pelagem")));
        carac.setFlagRaca(String.valueOf(request.getParameter("raca")));
        carac.setFlagEsterilizado(request.getParameter("saude_1") == null ? "N" : "S");
        carac.setFlagVacinado(request.getParameter("saude_2") == null ? "N" : "S");
        carac.setFlagVermifugado(request.getParameter("saude_3") == null ? "N" : "S");
        
        try {
            usr.setIdCaracteristica(carac.save());
            usr.save();
        } catch (SQLException ex) {
            url += "?error=true";
        }

        response.sendRedirect(url);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
